document.addEventListener('DOMContentLoaded', function() {
    const navLinks = document.querySelectorAll('nav ol li a');
    const sections = document.querySelectorAll('.banner, .div3, .services, section, .contactus');
    
    // 滚动监听函数
    function onScroll() {
        const scrollPos = window.scrollY + 100; // 偏移量

        sections.forEach(section => {
            const top = section.offsetTop;
            const bottom = top + section.offsetHeight;
            
            if (scrollPos >= top && scrollPos < bottom) {
                navLinks.forEach(link => {
                    link.parentElement.classList.remove('active');
                    if (link.getAttribute('data-target') === section.classList[0]) {
                        link.parentElement.classList.add('active');
                    }
                });
            }
        });

        // 导航栏背景透明度控制
        const nav = document.querySelector('nav');
        if (window.scrollY > 100) {
            nav.style.backgroundColor = 'rgba(35, 35, 35, 0.95)';
        } else {
            nav.style.backgroundColor = 'rgba(35, 35, 35, 0.8)';
        }
    }

    // 平滑滚动优化
    navLinks.forEach(link => {
        link.addEventListener('click', function(e) {
            e.preventDefault();
            const targetId = this.getAttribute('data-target');
            const targetElement = document.querySelector(`.${targetId}`);
            
            if (targetElement) {
                const offsetTop = targetElement.offsetTop;
                window.scrollTo({
                    top: offsetTop,
                    behavior: 'smooth'
                });
            }
        });
    });

    // 添加滚动监听
    window.addEventListener('scroll', onScroll);
    onScroll(); // 初始化时执行一次
});